package com.sunday.common.mq.kafka.study.spring.e20_Brave_Trace;

import org.apache.kafka.common.header.Header;
import org.apache.kafka.common.header.Headers;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * @Description: header 解析工具类
 * @Author: zsy
 * @Create: 2023/11/20 16:13
 */
public class HeaderUtils {

    /**
     * {@link io.micrometer.tracing.brave.bridge.W3CPropagation}
     */
    static final String TRACE_STATE = "tracestate";

    public static String lastStringHeader(Headers headers) {
        return lastStringHeader(headers, TRACE_STATE);
    }

    public static String lastStringHeader(Headers headers, String key) {
        Header header = headers.lastHeader(key);
        if (header == null || header.value() == null) return null;
        return new String(header.value(), UTF_8);
    }

}
